package com.itemp.wifidirectsend;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class WifiConfigSendTask extends AsyncTask <String, Integer, Boolean> {
    private final static String TAG = "WifiConfigSendTask";
    private static final int PORT = 4786;
    private String message;

    public WifiConfigSendTask(String message) {
        this.message = message;
    }

    @Override
    protected Boolean doInBackground(String... strings) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            socket = new Socket();
            socket.bind(null);
            socket.connect((new InetSocketAddress(strings[0], PORT)), 10000);
            outputStream = socket.getOutputStream();
            if (outputStream != null) {
                outputStream.write(message.getBytes());
            }
            outputStream.close();
            socket.close();
            outputStream = null;
            socket = null;
            return true;
        }catch (Exception e){
            Log.d(TAG, e.toString());
        }finally {
            try {
                if (outputStream != null){
                    outputStream.close();
                }
                if (socket != null){
                    socket.close();
                }
            }catch (Exception e){

            }
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        Log.e(TAG, "onPostExecute: " + aBoolean);
    }
}
